package com.springspirit.monitor.web;

import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.springspirit.framework.logging.LogAppender;

/**
 * 
 * @author grange
 *
 */
@Controller
public class ChartJsonController {
	
	private static final Logger logger = LoggerFactory.getLogger(ChartJsonController.class);
	
	@Autowired
	LogAppender logAppender;
	
	/**
	 * 
	 * @param locale
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/chartJson.do", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		
		logger.info("Chart Data Loading...");
		
		List logList = logAppender.getLogCount();
		
		StringBuilder jsonCount = new StringBuilder("[["); // [[1, 3, 2, 4, 6, 9]]
		int i = 0;
		
		for (Iterator iterator = logList.iterator(); iterator.hasNext();) {
			Map<String, Object> map = (Map<String, Object>) iterator.next();
			if(i > 0){				
				jsonCount.append(",");
			}
			
			jsonCount.append(map.get("count"));	
			i++;
		}
		jsonCount.append("]]");
		
		model.addAttribute("count", jsonCount.toString());
		
		if(logger.isDebugEnabled()){
			logger.debug("Chart Data: " + jsonCount.toString());
		}
		
		return "chart";
	}
	
}
